package helper
{
	import com.adobe.serialization.json.JSON;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;

	public class Services 
	{
		private const SHOOT_URL:String = "http://diemtuatinhthan.com/wp-content/game/manulife/index.php/game/shoot";
		private const GET_QUESTION_URL:String ="http://diemtuatinhthan.com/wp-content/game/manulife/index.php/game/getQuestion";
		private const ANSWER_QUESTION_URL:String ="http://diemtuatinhthan.com/wp-content/game/manulife/index.php/game/answer/";
		private const GET_STORE_URL:String ="http://diemtuatinhthan.com/wp-content/game/manulife/index.php/game/getStore";
		private const GET_INFO_URL:String ="http://diemtuatinhthan.com/wp-content/game/manulife/index.php/game/getInfo";
		private const START_URL:String = "http://diemtuatinhthan.com/wp-content/game/manulife/index.php/game/start";
		private const GET_INVITE_FRIEND_URL:String = "http://diemtuatinhthan.com/wp-content/game/manulife/index.php/game/getLink";
		private const LOG_URL:String = "http://diemtuatinhthan.com/wp-content/game/manulife/index.php/game/traceLog/";
		private var _shoot_loader:URLLoader;
		private var _get_question_loader:URLLoader;
		private var _answer_question_loader:URLLoader;
		private var _get_store_loader:URLLoader;
		private var _get_info_loader:URLLoader;
		private var _start_loader:URLLoader;
		private var _get_invite_friend_Loader:URLLoader;
		private var _trace_log_Loader:URLLoader;
		public static var instance:Services;
		public function Services()
		{
			if(!instance){
				instance = this;
			}
			init();
		}
		public static function getInstance():Services{
			if(!instance){
				instance = new Services();
			}
			return instance;
		}
		private function init():void{
			_shoot_loader = new URLLoader();
			_get_question_loader = new URLLoader();
			_answer_question_loader = new URLLoader();
			_get_question_loader = new URLLoader();
			_get_info_loader = new URLLoader();
			_start_loader = new URLLoader();
			_get_invite_friend_Loader = new URLLoader();
			_trace_log_Loader = new URLLoader();
		}
		public function shoot(result_f:Function,error_f:Function):void{
			var url_request:URLRequest = new  URLRequest(SHOOT_URL);
			_shoot_loader.addEventListener(Event.COMPLETE,result_f);
			_shoot_loader.addEventListener(IOErrorEvent.IO_ERROR,error_f);
			_shoot_loader.load(url_request);
		}
		public function getQuestion(result_f:Function,error_f:Function):void{
			var url_request:URLRequest = new  URLRequest(GET_QUESTION_URL);
			_get_question_loader.addEventListener(Event.COMPLETE,result_f);
			_get_question_loader.addEventListener(IOErrorEvent.IO_ERROR,error_f);
			_get_question_loader.load(url_request);
		}
		public function answerQuestion(result_f:Function,error_f:Function,answer:String):void{
			var url_request:URLRequest = new  URLRequest(ANSWER_QUESTION_URL+ answer);
			_answer_question_loader.addEventListener(Event.COMPLETE,result_f);
			_answer_question_loader.addEventListener(IOErrorEvent.IO_ERROR,error_f);
			_answer_question_loader.load(url_request);
		}
		public function getStore(result_f:Function,error_f:Function):void{
			var url_request:URLRequest = new  URLRequest(GET_STORE_URL);
			_get_question_loader.addEventListener(Event.COMPLETE,result_f);
			_get_question_loader.addEventListener(IOErrorEvent.IO_ERROR,error_f);
			_get_question_loader.load(url_request);
		}
		public function getinfo(result_f:Function,error_f:Function):void{
			var url_request:URLRequest = new  URLRequest(GET_INFO_URL);
			_get_info_loader.addEventListener(Event.COMPLETE,result_f);
			_get_info_loader.addEventListener(IOErrorEvent.IO_ERROR,error_f);
			_get_info_loader.load(url_request);
		}
		public function start(result_f:Function,error_f:Function):void{
			var url_request:URLRequest = new  URLRequest(START_URL);
			_start_loader.addEventListener(Event.COMPLETE,result_f);
			_start_loader.addEventListener(IOErrorEvent.IO_ERROR,error_f);
			_start_loader.load(url_request);
		}
		public function getInviteFriend(result_f:Function,error_f:Function):void{
			var url_request:URLRequest = new  URLRequest(GET_INVITE_FRIEND_URL);
			_get_invite_friend_Loader.addEventListener(Event.COMPLETE,result_f);
			_get_invite_friend_Loader.addEventListener(IOErrorEvent.IO_ERROR,error_f);
			_get_invite_friend_Loader.load(url_request);
		}
		public function traceLog(log:String):void{
			var url_request:URLRequest = new  URLRequest(LOG_URL+log);
			_trace_log_Loader.load(url_request);
		}
		
	}
}